// pages/accessrecord/accessrecord.js
import {scanCode} from "../../utils/asyncWx.js"
import {request, requestD} from "../../request/request.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs:[
      {
        id:0,
        value:"扫码记录",
        isActive:true
      },
      {
        id:1,
        value:"手动记录",
        isActive:false
      }
    ],
    info:{
      pid:'',
      gender:'',
      name:'',
      gender:''
    },
    // info:{
    //   pid:1,
    //   name:'张三',
    //   gender:0,
    //   phone:'13913857829',
    //   ptype: 3
    // },
    tempList:{
      int:[
        35,36,37,38,39,40
      ],
      dec:[
        0,1,2,3,4,5,6,7,8,9
      ]
    },
    temp:0.0,
    
  },

  //单选数据
  tempInt:0.0,
  tempDec:0.0,

  //初始化数据
  resetData(){
    this.setData({
      info:{
        name:'',
        gender:''
      }
    })
  },

  handleIntChange(e){
    this.tempInt = parseFloat(e.detail.value);
    this.setData({temp: this.tempInt+0.1*this.tempDec})
  },
  handleDecChange(e){
    this.tempDec = parseFloat(e.detail.value);
    if(this.tempInt!=0.0){
      this.setData({temp: this.tempInt+0.1*this.tempDec})
    }
  },

  //扫码
  async handleScan(e){
    try{
      let res1 = await scanCode()
      try{
        let scanInfo = JSON.parse(res1.result)
        scanInfo.ptype = 3
        this.setData({
          info: scanInfo
        })
      }catch(err2){
        wx.showToast({
          title: '无效二维码',
          icon: 'none',
          duration: 1500,
          mask: true,
        });
      }
    }catch(err1){
      console.log(err1)
    }
  },

  //确认提交
  handleRecord(e){
    if(this.data.info.name===''){
      wx.showToast({
        title: '您还未记录',
        icon: 'none',
        duration: 800
      });
      return
    }else if(this.data.temp===0.0){
      wx.showToast({
        title: '请输入体温',
        icon: 'none',
        duration: 800
      });
      return
    }else{
      const recorder_id = wx.getStorageSync("user").pid;
      requestD({
        url:'record/addRecord',
        data:{
          r_pid: this.data.info.pid,
          recorder_id,
          form: 1,
          temp: this.data.temp,
          form_addr:'湖北省武汉市洪山区',
          business:'软件园中路'
        }
      }).then(result=>{
        const resp = result.data
        if(resp.error_code!=0){
          console.log(resp)
          wx.showToast({
            title: '上传失败',
            icon: 'none',
            duration: 800
          })
        }else{
          wx.showToast({
            title: '提交成功',
            icon: 'done',
            duration: 800
          });
          this.resetData()
        }
      })

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.resetData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})